#include <autoconfig.h>
#include <stdio.h>
#include <string.h>
#include "port.h"
// #include "fpga-common.h"
#include "misc-if.h"
// #include "fpga-private.h"
#include "port.h"
#include <assert.h>

// char *pmagic = MAGIC_FPGA_IF;

#define INIT_BSP     (102)
#define LOAD_BSP_ENV (203)

#define MAX_IF       (30)
struct misc_if  misc_iface[MAX_IF] = { 0 };
struct misc_dev g_misc_if          = { 0 };
//////////////////////

int getChargeState(int *chargeState1, int *chargeState2, int *externalPowerSupply)
{
    assert(g_misc_if.interface != NULL);
    return g_misc_if.interface->pfun_getChargeState(chargeState1, chargeState2, externalPowerSupply);
}


int getBatteryPower(int *power1, int *power2)
{
    if (g_misc_if.interface && g_misc_if.interface->pfun_getBatteryPower) {
	g_misc_if.interface->pfun_getBatteryPower(power1, power2);
    }
}
void this_test(int a)
{
    // printf("show-----------\r\n");
}
void fun_test(int a)
{
    assert(g_misc_if.interface != NULL);
    return g_misc_if.interface->pfun_test(a);
}
int getTemperature(int *data)
{
    assert(g_misc_if.interface != NULL);
    return g_misc_if.interface->pfun_getTemperature(data);  // OK
}
int get_temp_vol(float *data)
{
    return g_misc_if.interface->pfun_get_temp_vol(data);  // OK
}
int setLCDBrightness(int brightness)
{
    assert(g_misc_if.interface != NULL);
    return g_misc_if.interface->pfun_setLCDBrightness(brightness);  // OK
}

int setBacklightTime(int backlightTime)
{
    assert(g_misc_if.interface != NULL);
    return g_misc_if.interface->pfun_setBacklightTime(backlightTime);  // OK
}

int setBacklightAlwaysOn(void)
{
    assert(g_misc_if.interface != NULL);
    return g_misc_if.interface->pfun_setBacklightAlwaysOn();  // OK
}

void misc_if_check(struct misc_if *interface)
{
    // if (NULL == interface->pfun_test) {
    // 	printf("pfun_test接口未定义\r\n");
    // }
}

int misc_if_register(struct misc_if *interface)
{
    int i;
    for (i = 0; i < MAX_IF; i++) {
	if (misc_iface[i].name &&
	    strcmp(interface->name, misc_iface[i].name) == 0) {
	    printf("接口已经存在\r\n");
	    return -1;
	}
    }
    for (i = 0; i < MAX_IF; i++) {
	if (misc_iface[i].name == NULL) {
	    break;
	}
    }
    // TODO 校验各个回调函数是否有效，正式程序可以不用
    misc_if_check(interface);
    memcpy(&misc_iface[i], interface, sizeof(struct misc_if));
    return 0;
}

int misc_if_unregister(char *name)
{
    int i;
    for (i = 0; i < MAX_IF; i++) {
	if (misc_iface[i].name && strcmp(name, misc_iface[i].name) == 0)
	{
	    misc_iface[i].name = NULL;
	    // printf("成功删除接口\r\n");
	    return -1;
	}
    }
}


struct misc_if *misc_if_append(char *name)
{
    // assert(name != NULL);

    int find = 0;
    int i;
    for (i = 0; i < MAX_IF; i++) {
	if (misc_iface[i].name && strcmp(name, misc_iface[i].name) == 0)
	{
	    misc_if_check(&misc_iface[i]);
	    return &misc_iface[i];
	}
    }
    printf("接口[%s]不存在\r\n", name);
    return NULL;
}


#if CONFIG_CPU_ORANGEPI
static char *env_interface = "this-common-fpga";
#else
static char *env_interface = "this-common";
#endif


static void _update_env()
{
    char *env = NULL;

    env = getenv("DEVICE_MISC_INTERFACE");
    if (env) {
	env_interface = env;
    }
    else {
	printf("未定义 DEVICE_MISC_INTERFACE，采用默认%s\r\n", env_interface);
	// exit(0);
    }
}

static void __attribute__((constructor(LOAD_BSP_ENV))) _load_env_misc_core()
{
    _update_env();
    g_misc_if.interface = misc_if_append(env_interface);
}
